$(function () {
	// 从sessionStorage获取goods的id
	const goodsId = sessionStorage.getItem("selectedProductId");

	// 检查goodsId是否存在
	if (!goodsId) {
		alert("商品ID未找到，请刷新页面或重新登录。");
		return;
	}

	// 使用axios发送GET请求获取商品详情
	axios
		.get(`http://localhost:9000/goods/item/${goodsId}`)
		.then(function (response) {
			// 检查响应码
			if (response.data.code === 1) {
				const goods = response.data.info;
				function updateProductInfo(goods) {
					setImgSrc(".middleimg", goods.img_big_logo);
					setText(".title", goods.title);
					setText(".old", goods.price, "未知");
					setText(".discount", goods.sale_type, "无");
					setText(".curprice", goods.current_price);
					setHTML(".desc", goods.goods_introduce, "暂无介绍");
				}

				// 使用
				updateProductInfo(goods);
			} else {
				// 响应码不是1，显示错误信息
				alert(response.data.message);
			}
		});
});

function setImgSrc(selector, src) {
	$(selector).attr("src", src);
}

function setText(selector, text, fallback = "") {
	$(selector).text(text || fallback);
}

function setHTML(selector, html, fallback = "") {
	$(selector).html(html || fallback);
}